import json
import pickle

import numpy as np

from config.prompts_version3 import *
from data.level_data import LevelData
from network.BaiduConnector import BaiduConnector
from utils.utils import extract_json_code_blocks, LOGGER

CLIENT = BaiduConnector()
SENDER = CLIENT.send_knowledge


def project_describe_version3(boss_name, my_name, project_name, my_task) -> str:
    # 生成对话内容
    message = prompt_project_describe_version3.format(boss_name, my_name, project_name, my_name, my_task, boss_name)
    response = SENDER(message)
    result = json.loads(response.data)
    LOGGER.info(f"{message} -- {result['result']}")
    return result['result']


def boss_get_intent(dialogs):
    # 生成对话内容
    message = prompt_boss_get_intent.format(dialogs)
    response = SENDER(message)
    result = json.loads(response.data)

    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'intent': 'unknown'}
    except:
        json_result = {'intent': 'unknown'}
    LOGGER.info(f"{message} -- {result['result']}")
    return json_result


def normal_response(role_name, dialogs):
    # 生成对话内容
    message = prompt_normal_response.format(role_name, dialogs)
    response = SENDER(message)
    result = json.loads(response.data)
    LOGGER.info(f"{message} -- {result['result']}")
    return result['result']


def format_response(role_name, content, dialogs):
    # 生成对话内容
    message = prompt_reformat_response.format(role_name, content, dialogs)
    response = SENDER(message)
    result = json.loads(response.data)
    LOGGER.info(f"{message} -- {result['result']}")
    return result['result']


def format_response_tones(role_name, content, dialogs):
    # 生成对话内容
    message = prompt_reformat_tones_response.format(role_name, content, dialogs)
    response = SENDER(message)
    result = json.loads(response.data)
    LOGGER.info(f"{message} -- {result['result']}")
    return result['result']

def get_task_name(dialogs):
    # 生成对话内容
    message = prompt_boss_get_job_name.format(dialogs)
    response = SENDER(message)
    result = json.loads(response.data)

    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'task_name': 'unknown', "reason": 'unknown'}
    except:
        json_result = {'task_name': 'unknown', "reason": 'unknown'}
    LOGGER.info(f"{message} -- {result['result']}")
    return json_result


def relationship_change(boss_name, my_name, relationship, dialogs):
    # 生成对话内容
    new_reationships = ','.join(['死对头', '相互不喜欢', '不熟', '喜欢', '密友'])

    message = prompt_relationship_change.format(boss_name, my_name, relationship, dialogs, new_reationships)
    response = SENDER(message)
    result = json.loads(response.data)

    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'is_change_relationship': 'unknown', 'new_relationship': 'unknown'}
    except:
        json_result = {'is_change_relationship': 'unknown', 'new_relationship': 'unknown'}
    LOGGER.info(f"{message} -- {result['result']}")
    return json_result


def boss_ask_job_agree(boss_name, my_name, relationship, ask_task, dialogs):
    message = prompt_boss_ask_job_agree.format(boss_name, my_name, relationship, ask_task, dialogs)

    response = SENDER(message)
    result = json.loads(response.data)

    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'intent': 'unknown', 'reason': 'unknown'}
    except:
        json_result = {'intent': 'unknown', 'reason': 'unknown'}
    LOGGER.info(f"{message} -- {result['result']}")
    return json_result


def boss_response(reason, act, ask_task, my_name, boss_name, dialogs):
    message = prompt_boss_response.format(reason, act, ask_task, my_name, boss_name, my_name, dialogs)
    response = SENDER(message)
    result = json.loads(response.data)
    LOGGER.info(f"{message} -- {result['result']}")
    return result['result']


# 假设的函数定义（需要您提供具体实现）
def colleague_get_intent(my_name, colleague_name, dialogs):
    # 生成对话内容
    message = prompt_colleague_get_intent.format(my_name, colleague_name, dialogs)
    response = SENDER(message)
    result = json.loads(response.data)

    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'intent': 'unknown', "reason": 'unknown'}
    except:
        json_result = {'intent': 'unknown', "reason": 'unknown'}
    LOGGER.info(f"{message} -- {result['result']}")
    return json_result


def colleague_ask_job_is_agree(colleague_name, my_name, relationship, task_name, current_financial_status,
                               future_financial_status):
    message = prompt_colleague_ask_job_is_agree.format(colleague_name, my_name, relationship,
                                                       task_name,
                                                       current_financial_status, future_financial_status)

    response = SENDER(message)
    result = json.loads(response.data)
    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'intent': 'unknown', "reason": 'unknown'}
    except:
        json_result = {'intent': 'unknown', "reason": 'unknown'}
    LOGGER.info(f"{message} -- {result['result']}")
    return json_result


def colleague_give_job_agree(my_name, colleague_name, relationship, task_name):
    message = prompt_colleague_give_job_agree.format(colleague_name, my_name, relationship, task_name)

    response = SENDER(message)
    result = json.loads(response.data)
    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'intent': 'unknown', 'reason': 'unknown'}
    except:
        json_result = {'intent': 'unknown', 'reason': 'unknown'}
    LOGGER.info(f"{message} -- {result['result']}")
    return json_result


def colleague_response(reason, task_name, act, my_name, colleague_name, dialogs):
    message = prompt_colleague_response.format(reason, task_name, act, my_name, colleague_name, my_name, dialogs)
    response = SENDER(message)
    result = json.loads(response.data)
    LOGGER.info(f"{message} -- {result['result']}")
    return result['result']


def test_colleague_want_job(level, name):
    # 生成对话内容
    df = level.get_task_people()
    df = df[~df['role'].isin(['boss', 'me'])]
    df = df[df['name_people'] != name]
    tasks = ','.join(df['name_task'].unique())

    message = prompt_colleague_want_job.format(name, tasks)
    response = SENDER(message)
    result = json.loads(response.data)
    LOGGER.info(f"{message} -- {result['result']}")
    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'task_name': 'unknown'}
    except:
        json_result = {'task_name': 'unknown'}
    return json_result


def test_colleague_willingness(level):
    # 生成对话内容
    df = level.get_task_people()
    currency_situation = str(level.calc_cash(df)['cash'].to_dict())
    df = df[~df['role'].isin(['boss', 'me'])]
    tasks = df.groupby("name_people")['name_task'].apply(lambda x: ','.join(x)).reset_index(name='tasks')
    data_jobs = str(tasks.set_index('name_people')['tasks'].to_dict())
    names = ','.join(df['name_people'].unique())
    message = prompt_colleague_willingness.format(currency_situation, data_jobs)
    response = SENDER(message)
    result = json.loads(response.data)
    LOGGER.info(f"{message} -- {result['result']}")
    try:
        json_result = extract_json_code_blocks(result['result'])
        if not json_result:
            json_result = {'name': 'unknown', "reason": 'unknown'}
    except:
        json_result = {'name': 'unknown', "reason": 'unknown'}
    return json_result
